//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type LocalPermDbItem struct {
	ProjectId     string `bson:"_id"`
	AccessChannel bool   `bson:"accessChannel"`
}

type _LocalPermDao struct {
	mc *mongo.Collection
}

func newLocalPermDao(db *mongo.Database) (*_LocalPermDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT + "local_perm")
	return &_LocalPermDao{mc: mc}, nil
}

func (db *_LocalPermDao) Set(ctx context.Context, dbItem *LocalPermDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.ProjectId}, bson.M{"$set": dbItem},
		options.Update().SetUpsert(true))
	return err
}

func (db *_LocalPermDao) Get(ctx context.Context, projectId string) (*LocalPermDbItem, error) {
	retItem := &LocalPermDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": projectId}).Decode(retItem)
	return retItem, err
}
